import contextlib

class Config:
    enable_backprop =  True

@contextlib.contextmanager
def using_config(attr_name, attr_value):
    old_value = getattr(Config, attr_name)     #预处理器，进入with块时调用
    setattr(Config,attr_name, attr_value)
    try:
        yield
    finally:     #后处理器，离开with块时调用
        setattr(Config, attr_name, old_value)

def no_grad():
    return using_config('enable_backprop', False)